package com.example.administrator.familfinalcialmanagement;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.bean.LoginBean;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityMainBinding;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.ui.RegisterActivity;
import com.example.administrator.familfinalcialmanagement.ui.activity.ForgetPwdActivity;
import com.example.administrator.familfinalcialmanagement.util.ApiHTTP;
import com.example.administrator.familfinalcialmanagement.util.GsonUtil;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.PermissionUtil;
import com.example.administrator.familfinalcialmanagement.util.PhontNumberUtil;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

import static com.example.administrator.familfinalcialmanagement.Constants.LOGINPRESSION;
import static com.example.administrator.familfinalcialmanagement.Constants.REQEEST_CDOE_GPS;

public class MainActivity extends BaseActivity {

    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        String isLogin = (String) SpUtils.get("isLogin","");
        if ("1".equals(isLogin)) {
            startActivity(new Intent(MainActivity.this,GRActivity.class));
            finish();
        }

        //动态申请权限
        initPermission();
        binding.textViewRegister.setOnClickListener(this);
        binding.textViewMain.setOnClickListener(this);
    }

    private void initPermission() {
        if (PermissionUtil.loginPermission(this, LOGINPRESSION)) {
            if (!PermissionUtil.isOPen(MainActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, REQEEST_CDOE_GPS);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textViewRegister:
                startActivity(new Intent(MainActivity.this, RegisterActivity.class));
                break;
            case R.id.textViewMain:
                String phone = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入账号");
                    return;
                }

                String pwd = binding.pwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入密码");
                    return;
                }

                ApiHTTP.getInstance().login(phone, pwd, new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LoginBean bean = GsonUtil.GsonToBean(s,LoginBean.class);
                        if (0 == bean.getCode()) {
                            SpUtils.putBean("module_loginBean",bean);
                            startActivity(new Intent(MainActivity.this, GRActivity.class));
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        ToastUtil.showToast("登录失败");
                    }
                });

                break;
            default:
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return true;
    }
}
